package com.logos.cartoon.Task;

import cn.hutool.core.convert.impl.CharacterConverter;
import com.alibaba.fastjson.JSON;
import com.logos.cartoon.constant.CharacterConstant;
import com.logos.cartoon.controller.CharacterInfoController;
import com.logos.cartoon.converter.CharacterInfoConverter;
import com.logos.cartoon.entity.CharacterInfo;
import com.logos.cartoon.mapper.CharacterInfoMapper;
import com.logos.cartoon.redis.RedisUtil;
import com.logos.cartoon.resp.CharacterInfoResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 定时任务类，定时推荐每日角色
 *
 * @author rose
 * @date 2025/03/19
 */
@Component
@Slf4j
public class DayCharacterTask {

    @Resource
    private CharacterInfoMapper characterInfoMapper;

    @Resource
    private RedisUtil redisUtil;

    public static final String CHARACTER_INFO_LIST = "character:info:list";

    public static final String CHAR_LIKED_COUNT_KEY = "character:liked:count";

    public static final String DAY_CHARACTER_KEY = "character:day:";

    // 每分钟执行
    @Scheduled(cron = "0 * * * * ? ")
//    @Scheduled(cron = "0 0 0 * * ? ") 每天0点执行
    public void randomCharacter() {
        // 先从缓存或者数据库中查询出total数据
        List<CharacterInfoResp> characterInfoRespList = new ArrayList<>();
        if (redisUtil.exist(CHARACTER_INFO_LIST)) {
            String characterInfoListStr = redisUtil.get(CHARACTER_INFO_LIST);
            characterInfoRespList = JSON.parseArray(characterInfoListStr, CharacterInfoResp.class);
        } else {
            List<CharacterInfo> characterInfos = characterInfoMapper.selectByLimit(CharacterConstant.ALL_LIMIT);
            characterInfoRespList = EntityToRespList(characterInfos);
            // 缓存数据,有效期三分钟
            redisUtil.setNx(CHARACTER_INFO_LIST, JSON.toJSONString(characterInfos), 3L, TimeUnit.MINUTES);
        }
        // 随机获取一个角色
        CharacterInfoResp characterInfoResp = characterInfoRespList.get((int) (Math.random() * characterInfoRespList.size()));
        // 设置角色的点赞数和收藏数
        Integer count = redisUtil.getInt(CHAR_LIKED_COUNT_KEY + ":" + characterInfoResp.getId());
        count = Objects.isNull(count) ? 0 : count;
        characterInfoResp.setLikeCount(Integer.valueOf(count));
        // 将当前角色数据缓存到redis中
        String key = DAY_CHARACTER_KEY + new Date().getDate();
        log.info("day task character is {}", characterInfoResp);
        redisUtil.set(key, JSON.toJSONString(characterInfoResp));
    }

    public List<CharacterInfoResp> EntityToRespList(List<CharacterInfo> characterInfos) {
        List<CharacterInfoResp> characterInfoRespList = new ArrayList<>();
        for (CharacterInfo characterInfo : characterInfos) {
            CharacterInfoResp characterInfoResp = CharacterInfoConverter.INSTANCE.convertEntityToResp(characterInfo);
            characterInfoRespList.add(characterInfoResp);
        }
        return characterInfoRespList;
    }

}